package com.a;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Scroller;

/**
 * Created by Administrator on 2017/12/19.
 */

public class SlideMenu extends ViewGroup {
    View leftMenu;
    View mainContent;
    float downX, downY;
    float moveX;
    int currentState;//当前模式
    public static final int MAIN_STATE = 0;
    public static final int MENU_STATE = 1;
    private Scroller scroller;
    // float downX ,downY;

    public SlideMenu(Context context) {
        super(context);
        init();
    }

    public SlideMenu(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public SlideMenu(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        scroller = new Scroller(getContext());// FIXME: 2017/12/20 getContext()
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //指定左面板的宽高

        leftMenu = getChildAt(0);
        leftMenu.measure(leftMenu.getLayoutParams().width, heightMeasureSpec);
        //指定主面板的宽高
        mainContent = getChildAt(1);
        mainContent.measure(widthMeasureSpec, heightMeasureSpec);

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    /**
     * @param changed 当前控件尺寸大小,位置是否发生变化
     * @param l       当前控件的左边距
     * @param t
     * @param r
     * @param b
     */
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        leftMenu.layout(-leftMenu.getMeasuredWidth(), 0, 0, b);
        mainContent.layout(l, t, r, b);
    }

    /**
     * scrollTo(x,y)  直接跳到某个位置
     * scrollTo(20,y)
     * scrollTo(80,y)
     * scrollTo(-100,y)
     * <p>
     * <p>
     * scrollBy(x,y)  在原来基础上滚动
     * scrollBy(20,y)
     * scrollBy(80,y)
     * scrollBy(-100,y)
     * scrollBy(-20,y)
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:

                downX = event.getX();
                //scrollTo();
                break;
            case MotionEvent.ACTION_MOVE:

                moveX = event.getX();
                int scrollX = (int) (downX - moveX);  //
                Log.e("执行", "onTouchEvent: scrollX:" + scrollX);

                //计算将要滚动到的位置,判断是否超出边界,超出了就不执行scrollBy方法
                int nextScrollPosition = getScrollX() + scrollX;  //现在的位置 + 将要滚动的距离 =将要到达的位置
                //限定左边界
                if (nextScrollPosition < -leftMenu.getMeasuredWidth()) {
                    scrollTo(-leftMenu.getMeasuredWidth(), 0);

                } else if (nextScrollPosition > 0) {
                    scrollTo(0, 0);
                } else {
                    scrollBy(scrollX, 0);  // FIXME: 2017/12/20 scrollBy与scrollTo
                }


                downX = moveX;

                break;
            case MotionEvent.ACTION_UP:
                //根据当前滚动的位置和左面板的一半进行比较
                int leftCenter = (int) (-leftMenu.getMeasuredWidth() / 2.0f);
                if (getScrollX() < leftCenter) {
                    //打开

                    currentState = MENU_STATE;
                    updateCurrentContent();
                } else {
                    //关闭
                    currentState = MAIN_STATE;
                    updateCurrentContent();
                }

                break;
        }
        return true;  //fixme 没有继承控件的时候用ture
    }

    private void updateCurrentContent() {
        int startX = getScrollX();
        //当前位置 - 开始位置    -240 - (-100) = -140
        int dx = 0;
        if (currentState == MENU_STATE) {
            //切换菜单面板
            //  scrollTo(-leftMenu.getMeasuredWidth(), 0);
            dx = -leftMenu.getMeasuredWidth() - startX;  // FIXME: 2017/12/20 负的

        } else if (currentState == MAIN_STATE) {
            //切换主面板
            //  scrollTo(0, 0);
            dx = 0 - startX;
        }
        //1.开始平滑的数据模拟
        int duration = Math.abs(dx * 2);
        scroller.startScroll(startX, 0, dx, 0, duration);

        invalidate();//重绘界面 ->drawChild() -> computeScroll()  fixme 滑动的原因
    }

    /**
     * 2.维持动画的继续
     */
    @Override
    public void computeScroll() {
        super.computeScroll();
        //true,动画还没有结束
        if (scroller.computeScrollOffset()) {
            //获取当前模拟的数据,也就是要滚动到的位置
            int currX = scroller.getCurrX();
            scrollTo(currX, 0);
            invalidate();
        }
    }

    private void close() {
        currentState = MAIN_STATE;
        updateCurrentContent();
    }

    private void open() {
        currentState = MENU_STATE;
        updateCurrentContent();
    }

    public void switchState() {
        if (currentState == MENU_STATE) {
            close();
        } else {
            open();
        }
    }


    /**
     * 分发方法存在的原因是防止左面板中的scrollView消耗滑动事件,(在scrollView中左右滑动没有响应)所以在水平方向做了拦截
     */
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {

            case MotionEvent.ACTION_DOWN:
                downX = ev.getX();
                downY = ev.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                float xOffset = Math.abs(ev.getX() - downX);
                float yOffset = Math.abs(ev.getY() - downY);

                if (xOffset > yOffset && xOffset > 5) {
                    return true;
                }

                break;
            case MotionEvent.ACTION_UP:

                break;
        }
        return super.onInterceptTouchEvent(ev);
    }
}
